$(window).resize(function () {
    sideBarPosition();
});
sideBarPosition();
(function () {
    var timer = null;
    $('#service-list').css({
        top: -($('#service-list').children().length * 30 - 30)
    });
    $('.contact-service').hover(function () {
        clearTimeout(timer);
        $('#service-list').stop(false, true);
        $('#service-list').show().animate({ left: -100, opacity: 1 }, 200);
    }, function () {
        timer = setTimeout(function () {
            $('#service-list').stop(false, true);
            $('#service-list').animate({ left: 0, opacity: 0 }, 200, function () {
                $(this).hide();
            });
        }, 500);
    });
})();
//sideBar定位
function sideBarPosition() {
    var nWinW = $(window).width();
    if (nWinW <= 1200) {
        $('#sidebar').css({ right: -35 });
        $('.sidebar-main-bottom, .shopping-cart').css({ left: -35 });
    }
    else {
        $('#sidebar').css({ right: 0 });
        $('.sidebar-main-bottom, .shopping-cart').css({ left: 0 });
    }
}


var _oCartTimes = null;
//加入封装加入购物车
function productAddCart(_this, num, productId, imgUrl) {
    if (num <= 0)
        return;
    if (_oCartTimes) clearTimeout(_oCartTimes);
    hideAddCartSuccessDialog();
    Module.animatManger.createImgAndMoveToTarget({
        startX: _this.offset().left,
        startY: _this.offset().top,
        endX: $(window).width() - 35,
        endY: $('.shopping-cart').offset().top,
        imgUrl: imgUrl,
        imgHeight: '30px',
        imgWidth: '30px',
        callBack: function () {
            $('.animate-num').text(num).show().css({ opacity: 1 }).animate({ top: -10, opacity: 0 }, 200, function () {
                $(this).hide().css({ opacity: 0, top: 15 });
                //									var nAddNum = parseInt($('.cart-num').text()) + num;
                showAddCartSuccessDialog();
                refreshCartHtml();
            });
        }
    });
}
//显示添加购物车成功提示
function showAddCartSuccessDialog() {
    $('#dialog-add-cart-success').show();
    setTimeCloseAddCartSuccessDialog();
    $('#dialog-add-cart-success .close').one('click', function () {
        hideAddCartSuccessDialog();
    });
}
//隐藏添加购物车成功提示
function hideAddCartSuccessDialog() {
    $('#dialog-add-cart-success').hide();
}
//定时隐藏添加购物车成功提示
function setTimeCloseAddCartSuccessDialog(time) {
    if (time === void 0) { time = 5000; }
    _oCartTimes = setTimeout(function () {
        hideAddCartSuccessDialog();
    }, time);
}
$(function () {
    sideBarPosition();
    $('.sidebar-main li a').hover(function () {
        var $_tip = $('.mui-mbar-tab-tip', $(this).parent());
        if ($_tip.length) {
            $_tip.show().animate({ opacity: 1, right: 35 }, 500);
        }
    }, function () {
        var $_tip = $('.mui-mbar-tab-tip', $(this).parent());
        if ($_tip.length) {
            $_tip.stop(true, true);
            $_tip.show().animate({ opacity: 0, right: 70 }, 500);
        }
    });
    //初始化购物车的数量
    //			refreshCartHtml(Module.cartManager.getCartNumForCookie());
    //移入时刷新购物车数量
    $('#sidebar .shopping-cart').mouseenter(refreshCart);
    (function () {
        var panel = Module.Panel();
        var observer = null;
        //打开bar的面板
        $('.open-panel').click(function (event) {
            //如果是打开历史浏览记录
            if (!panel.isOpen) {
                if ($(this).hasClass('panel-history')) {
                    var Observer = function () {
                        var temp = '{{each list as v}}' + '<li class="panel-item">' + '<a class="pic" target="_blank" href="{{v.path}}"  title="{{v.name}}" >' + '<img class="lazy" data-original="{{v.image_default | postfix:"100x100"}}" postfix="100x100"/></a>' + '<p class="item-name">' + '<a href="{{v.path}}" target="_blank" title="{{v.name}}">{{v.name}}</a>' + '</p>' + '<p class="item-name">' + '<a href="javascript:;" title="{{v.manufacturer}}" target="_blank">{{v.manufacturer}}</a>' + '</p>' + '<p class="item-price">' + '<a>￥{{v.price}}</a>' + '</p>' + '</li>' + '{{/each}}';
                        //'<p class="item-price">' + '{{if v.isLogin}}' + '<a>￥{{v.price}}</a>' + '{{else}}' + '<a href="javascript:;" class="login_look">登陆可见</a>' + '{{/if}}' + '</p>'
                        var _this = this;
                        this.update = function (data) {
                            this.container = data;
                            if (data.isOpen) {
                                this.present();
                            }
                            else {
                                $('.panel-wrap-history ul li').remove();
                                $('.panel-wrap-history').hide();
                                this.container.waitFinish();
                            }
                        };
                        this.present = function () {
                            Module.request.ajax({
                                url: base + "/history.jhtml",
                                success: function (data) {
                                    _this.container.finish();
                                    var d = { list: data };
                                    var render = template.compile(temp);
                                    var html = render(d);
                                    $('.panel-wrap-history ul').append(html);
                                    $('.history-count').text(data.length);
                                    $('.panel-wrap-history').show();
                                    $('.panel-wrap-history .panel-content').show();
                                    $('.panel-wrap-history img.lazy').lazyload({threshold:500,failure_limit:20});
                                    _this.resize();
                                },
                                error: function (data) {
                                    _this.container.finish();
                                    $('.panel-wrap-history').show();
                                    $('.panel-wrap-history .panel-content').hide();
                                    $('.panel-wrap-history .empty').show();
                                }
                            });
                        };
                        this.resize = function () {
                            var nWinHeight = $(window).height(), l = $('.panel-wrap-history ul li').length;
                            if (l * 126 + 40 > nWinHeight - 50) {
                                $('.panel-wrap-history .panel-content').css({
                                    height: nWinHeight - 50,
                                    overflowY: 'auto'
                                });
                            }
                            else {
                                $('.panel-wrap-history .panel-content').css({
                                    height: l * 126 + 40,
                                    overflowY: 'hidden'
                                });
                            }
                        }, this.clean = function (domain) {
                            $('.panel-wrap-history ul li').remove();
                            $('.panel-wrap-history .panel-content').hide();
                            $('.panel-wrap-history .empty').show();
                            $.cookie('_hgicn', ',', { expires: -1, path: '/', domain: domain }); //清空cookie
                        };
                    };
                }
                observer = new Observer();
                panel.cleanObserver();
                panel.addObserver(observer);
                $(this).addClass('selected');
                panel.open();
            }
            else {
                $('.bar-panel-close').trigger('click');
            }
            var event = event || window.event;
            event.stopPropagation();
        });
        $('#sidebar').click(function (event) {
            var event = event || window.event;
            var r = event.src || event.target;
            //如果是登陆可以见按钮的话 则让事件向上传播
            if (!$(r).hasClass('login_look')) {
                var event = event || window.event;
                event.stopPropagation();
            }
        });
        $(document).click(function (event) {
            $('.open-panel').removeClass('selected');
            panel.close();
        });
        //清空浏览记录
        $('.cleanBroswerHistory').click(function (event) {
            var domain = $(this).attr('domain');
            event = event || window.event;
            oDialog.confirm(
               '确认清空浏览记录吗?',
                function () {
                    if (observer)
                        observer.clean(domain);
                
                    event.stopPropagation();
                },
                function () {
//                    var event = event || window.event;
                    event.stopPropagation();
                }
            );
        });
        //关闭bar的面板
        $('.bar-panel-close').click(function () {
            $('.open-panel').removeClass('selected');
            panel.close();
        });
        $(window).resize(function () {
            if (observer)
                observer.resize();
        });
        var oScroll = {};
        //返回顶部
        $('.go_top').click(function () {
            oWinScroll.fnAnimateScrollTop();
        });
        //window 滚动
        var oWinScroll = {
            bIsAnimate: false,
            oTimer: null,
            fnScrollTop: function (nScroll) {
                if (typeof nScroll == 'undefined')
                    return $(window).scrollTop();
                else
                    $(window).scrollTop(nScroll);
            },
            fnAnimateScrollTop: function (nSpeed, nTarget) {
                var arg = arguments;
                if (arg.length === 0) {
                    nTarget = 0;
                    nSpeed = Math.floor((nTarget - this.fnScrollTop()) / 100);
                }
                this.bIsAnimate = true;
                var nScroll = this.fnScrollTop() + nSpeed < nTarget ? nTarget : this.fnScrollTop() + nSpeed;
                this.fnScrollTop(nScroll);
                if (this.fnIsFinish(nTarget))
                    return false;
                var _this = this;
                oScroll.oTimer = setTimeout(function () {
                    _this.fnAnimateScrollTop(nSpeed, nTarget);
                }, 1);
            },
            fnIsFinish: function (nTarget) {
                return this.fnScrollTop() == nTarget;
            }
        };
        $(window).scroll(function () {
            if (oWinScroll.fnScrollTop() > 0)
                $('.go_top').css('display', 'block');
            else
                $('.go_top').hide();
            //判断是人为滚动还是定时器滚动
            if (!oWinScroll.bIsAnimate)
                clearTimeout(oScroll.oTimer);
            oWinScroll.bIsAnimate = false;
        });
    })();
});

